package com.zkingsoft.services.lin.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.constance.SystemConstance;
import com.zkingsoft.dao.lin.LinOrderDao;
import com.zkingsoft.dao.lin.LinOrderDetailsDao;
import com.zkingsoft.dao.lin.LinOrderLogDao;
import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.model.lin.LinOrder;
import com.zkingsoft.model.lin.LinOrderDetails;
import com.zkingsoft.model.lin.LinOrderLog;
import com.zkingsoft.model.sys.SysUsers;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.services.lin.LinOrderService;
import com.zkingsoft.util.ModelUtils;
import com.zkingsoft.util.MoneyUtil;
import com.zkingsoft.util.StringUtils;
import com.zkingsoft.util.WebUtil;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2017-05-08 15:14
 */
@Service("linOrderService")
public class LinOrderServiceImpl implements LinOrderService {

	@Autowired
	private LinOrderDao linOrderDao;

	@Autowired
	private LinOrderDetailsDao linOrderDetailsDao;
	
	@Autowired
	private LinOrderLogDao linOrderLogDao;
	
	
	@Override
	public int add(LinOrder linOrder) {

		return linOrderDao.insert(linOrder);

	}

	@Override
	public int batchAdd(List<LinOrder> linOrderList) {
		int num = 0;

		int c = 10000;
		int size = linOrderList.size() / c + 1;
		for (int i = 0; i < size; i++) {
			int begin = i * c;
			int end = (i + 1) * c;
			end = end >= linOrderList.size() ? linOrderList.size() : end;
			List<LinOrder> insertList = linOrderList.subList(begin, end);
			num += linOrderDao.batchInsert(insertList);
		}
		return num;

	}

	@Override
	public int modifyByMap(LinOrder oldLinOrder, LinOrder newLinOrder) {

		if (!ModelUtils.isModified(oldLinOrder, newLinOrder)) {
			return 1;
		}
		Map<String, Object> modifyMap = null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldLinOrder, newLinOrder);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("orderId", oldLinOrder.getOrderId());
			linOrderDao.updateByMap(modifyMap);
		}
		return 1;
	}

	@Override
	public int modifyByModel(LinOrder linOrder) {

		return linOrderDao.updateByModel(linOrder);

	}

	@Override
	public int remove(List<Long> list) {

		return linOrderDao.deleteByIds(list);

	}

	@Override
	public int removeById(Long orderId) {

		return linOrderDao.deleteById(orderId);

	}

	@Override
	public int removeByModel(LinOrder linOrder) {

		return linOrderDao.deleteByModel(linOrder);

	}

	@Override
	public List<LinOrder> findInPage(LinOrder linOrder, PaginationVO pageVo) {

		return linOrderDao.selectInPage(linOrder, pageVo);

	}

	@Override
	public List<LinOrder> findByModel(LinOrder linOrder) {

		return linOrderDao.selectByModel(linOrder);

	}

	@Override
	public int findTotal(LinOrder linOrder) {

		return linOrderDao.selectTotalRecord(linOrder);

	}

	@Override
	public LinOrder findById(Long orderId) {

		return linOrderDao.selectById(orderId);

	}
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public int createOrder(LinOrder linOrder) {
		
		List<LinOrderDetails> details = linOrder.getLinOrderDetails();
		//生成随机订单编号，记录订单表
		linOrder.setOrderState(SystemConstance.ORDER_STATE_CREATE);
		linOrder.setOrderNum(StringUtils.getRandomString(10));
		linOrder.setOrderCreatetime(new Date());
		int i = linOrderDao.insert(linOrder);
		if(i <= 0) {
			throw new GlobleException("订单表插入失败！");
		}
		Long orderId = linOrder.getOrderId();
		//记录订单详细表
		for(LinOrderDetails detail : details) {
			detail.setOrderId(orderId);
			detail.setDetailsTotalPrice(MoneyUtil.mul(detail.getDetailsNum(), detail.getTypePrice()));
			detail.setDetailsRealPrice(MoneyUtil.mul(detail.getDetailsNum(), detail.getRealPrice()));
		}
		int j = linOrderDetailsDao.batchInsert(details);
		if(j <= 0) {
			throw new GlobleException("订单详细表插入失败！");
		}
		//记录订单日志表
		LinOrderLog orderLog = new LinOrderLog();
		orderLog.setOrderId(orderId);
		orderLog.setOrderState(SystemConstance.ORDER_STATE_CREATE);
		orderLog.setOrderRemark(linOrder.getOrderRemark());
		
		SysUsers loginUser = (SysUsers) WebUtil.getSessionAttribute(SystemConstance.LOGIN_KEY);
		orderLog.setOrderOperator(loginUser.getSuName());
		orderLog.setOrderOperattime(new Date());
		
		int k = linOrderLogDao.insert(orderLog);
		if(k <= 0) {
			throw new GlobleException("订单日志表插入失败！");
		}
		
		return j;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public int modifyOrder(LinOrder oldLinOrder, LinOrder newLinOrder) {
		
		if (!ModelUtils.isModified(oldLinOrder, newLinOrder)) {
			return 1;
		}
		Map<String, Object> modifyMap = null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldLinOrder, newLinOrder);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("orderId", oldLinOrder.getOrderId());
			linOrderDao.updateByMap(modifyMap);
			
			//记录订单日志表
			LinOrderLog orderLog = new LinOrderLog();
			orderLog.setOrderId(newLinOrder.getOrderId());
			orderLog.setOrderState(newLinOrder.getOrderState());
			orderLog.setOrderRemark(newLinOrder.getOrderRemark());
			
			SysUsers loginUser = (SysUsers) WebUtil.getSessionAttribute(SystemConstance.LOGIN_KEY);
			orderLog.setOrderOperator(loginUser.getSuName());
			orderLog.setOrderOperattime(new Date());
			
			linOrderLogDao.insert(orderLog);
		}
		
		return 1;
		
	}
	
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public int modifyLoss(LinOrder linOrder) {
		
		Integer totalLoss = 0; //总损耗
		//记录订单详细损耗
		List<LinOrderDetails> details = linOrder.getLinOrderDetails();
		for(LinOrderDetails detail : details) {
			if(detail.getDetailsId() != null) {
				totalLoss += detail.getDetailsLoss();
				linOrderDetailsDao.updateByModel(detail);
			}
		}
		
		//更新订单总损耗
		linOrder.setOrderTotalLoss(totalLoss);
		linOrderDao.updateByModel(linOrder);
		
		//记录订单日志表
		LinOrderLog orderLog = new LinOrderLog();
		orderLog.setOrderId(linOrder.getOrderId());
		orderLog.setOrderState(linOrder.getOrderState());
		orderLog.setOrderRemark(linOrder.getOrderRemark());
		
		SysUsers loginUser = (SysUsers) WebUtil.getSessionAttribute(SystemConstance.LOGIN_KEY);
		orderLog.setOrderOperator(loginUser.getSuName());
		orderLog.setOrderOperattime(new Date());
		
		int k = linOrderLogDao.insert(orderLog);
		if(k <= 0) {
			throw new GlobleException("订单日志表插入失败！");
		}
		
		return k;
	}
	
	@Override
	@Transactional(rollbackFor=Exception.class)
	public int editOrderState(Long orderId, String orderState, SysUsers loginUser) {
		LinOrder linOrder = linOrderDao.selectById(orderId);
		if(orderState.equals(linOrder.getOrderState())) {
			return 1;
		}
		linOrder.setOrderState(orderState);
		linOrderDao.updateByModel(linOrder);
		
		//记录订单日志表
		LinOrderLog orderLog = new LinOrderLog();
		orderLog.setOrderId(orderId);
		orderLog.setOrderState(orderState);
		orderLog.setOrderRemark(linOrder.getOrderRemark());
		
		orderLog.setOrderOperator(loginUser.getSuName());
		orderLog.setOrderOperattime(new Date());
		
		int k = linOrderLogDao.insert(orderLog);
		return k;
	}

	@Override
	public List<LinOrder> findTotalByMonth(LinOrder linOrder,
			PaginationVO pageVo) {
		return linOrderDao.selectTotalByMonth(linOrder, pageVo);
	}

	@Override
	public int findRecordByMonth(LinOrder linOrder) {
		return linOrderDao.selectRecordByMonth(linOrder);
	}
	
}